﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Xml;

namespace TwentyTree4Umbraco.Controls
{
    public class VideoUploader : Panel
    {
        private FileUpload fu;
        private Label status;
        private HiddenField txtValue;

        protected override void OnInit(EventArgs e)
        {
            fu = new FileUpload();
            this.Controls.Add(fu);

            status = new Label();
            this.Controls.Add(status);

            txtValue = new HiddenField();
            this.Controls.Add(txtValue);
            base.OnInit(e);

        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (this.Value != string.Empty)
            {
                try
                {
                    Data d = new Data();
                    XmlNode photos = d.GetPhotos();

                    XmlNode selected = photos.SelectSingleNode(string.Format("//photos [photo_id = '{0}']", this.Value));

                    string selectedtitle = selected.SelectSingleNode("./title").FirstChild.Value;
                    string thumbnail = selected.SelectSingleNode("./quad100_download").FirstChild.Value;

                    //string selectedtitle = this.Value;

                    writer.WriteLine(String.Format(
                       "<div id=\"videoUploaded{0}\"> <div class=\"vidImage\"><img src=\"{3}\"</div> video uploaded: <span id=\"videoId\">{1} ({2})</span> <a style=\"color:red\" href=\"#\" onclick=\"document.getElementById('{0}').value = ''; jQuery('#videoUploaded{0}').hide();\">Remove</a></div>",
                       txtValue.ClientID,
                       selectedtitle,
                       this.Value,
                       "http://" + Config.GetSetting("domain") + thumbnail));

                }
                catch
                {
                    writer.WriteLine((String.Format(
                         "<div id=\"videoUploaded{0}\">video processing</div>",
                         txtValue.ClientID)));
                }
            }

            base.Render(writer);
        }
        public void SaveFile()
        {

            if (fu.HasFile)
            {
                string path = "/media/weare/" + DateTime.Now.Ticks.ToString();


                fu.SaveAs(HttpContext.Current.Server.MapPath(path + fu.FileName));


                List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();

                if(AlbumID != "")
                    list.Add(new KeyValuePair<string, string>("album_id", AlbumID));
                string api_signature = Signature.Create(list);

                MultipartForm form;
                form = new MultipartForm("http://" + Config.GetSetting("domain") + "/api/photo/upload");
                form.FileContentType = "application/octet-stream";

                if (AlbumID != "")
                    form.setField("album_id", AlbumID);
                
                form.setField("api_signature", api_signature);
                form.sendFile(HttpContext.Current.Server.MapPath(path + fu.FileName));
                string response = form.ResponseText.ToString();


                XmlDocument xd = new XmlDocument();
                xd.LoadXml(response);

                if (xd.SelectSingleNode("/response").Attributes["status"].Value == "ok")
                {
                    status.Text = "Video Uploaded";
                    Value = xd.SelectSingleNode("/response/photo_id").InnerText;
                }
                else
                {
                    status.Text = xd.SelectSingleNode("/response").Attributes["message"].Value;

                }

            }
        }


        public string AlbumID { get; set; }

        public string Value
        {

            get
            {
                return txtValue.Value;
            }

            set
            {
                txtValue.Value = value;
            }
        }
    }
}
